package com.xxx.auth.common.util;

import com.xxx.auth.common.web.exception.DataNotFoundException;
import com.xxx.auth.entity.enums.Status;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Objects;
import java.util.function.Function;

public class DataUtil {

    public static <T, ID> T getData(JpaRepository<T, ID> repository, ID id, Function<T, Status> statusGetter) {
        T data = repository.findById(id).orElseThrow(() -> new DataNotFoundException("Cannot find the id: " + id));
        if (!Objects.equals(Status.NORMAL, statusGetter.apply(data))) {
            throw new DataNotFoundException("The status of the object is not 'NORMAL'");
        }
        return data;
    }

}
